package cc.liyinghao.common.exception;

import cc.liyinghao.common.api.Result;
import cn.hutool.json.JSONUtil;
import lombok.Getter;

import java.util.HashMap;

@Getter
public class ParameterException extends RuntimeException {
    private int code;
    private HashMap<String, Object> param = new HashMap<>();

    public ParameterException(String message) {
        super(message);
        this.code = Result.ResponseEnum.FAIL.getType();
    }

    public ParameterException(int code, String message) {
        super(message);
        this.code = code;
    }

    /***
     * 添加异常信息 键值对
     */
    public ParameterException param(String key, Object value) {
        param.put(key, value);
        return this;
    }

    /***
     * 置空param
     */
    public ParameterException empty() {
        this.param.clear();
        return this;
    }

    public String paramToString() {
        return param.isEmpty() ? null : JSONUtil.toJsonStr(param);
    }
}
